home *** CD-ROM | disk | FTP | other *** search
- /* cvspk.c
- *************************************************************************/
-
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
-
- #include "cvmsg.h"
- #include "cvobj.h"
- #include "cvlocs.h"
- #include "cvocab.h"
-
- #define TRUE (1)
- #define FALSE (0)
-
- extern struct hint hints[];
- extern struct cvocab vtab[];
- extern char *rmsg[] ;
- extern char *mmsg[] ;
- extern struct cmsg ctext[] ;
- extern int blklin, mltcmd, samvrb ;
-
- extern void bug();
- extern void exit();
-
- char code[] = { 'B'|0x80, 'a'|0x80, 'r'|0x80, 'n'|0x80};
-
- void
- encode(msg) register char *msg;
- { register int i;
- for (i=0; msg[i]; i++) {
- msg[i] ^= code[i & 3];
- }
- }
-
- void
- putcode(msg) register char *msg;
- { register int i;
- register char c;
- if (*msg & 0x80) {
- for (i=0; c=msg[i]; i++) {
- putchar(c ^ (code[i & 3]));
- }
- } else {
- (void) fputs(msg,stdout);
- }
- }
-
- void
- rspeak(num) register int num;
- { register char *msg ;
- msg = rmsg[num] ;
- if (num == 54) blklin = FALSE ;
- if (*msg == '*') {
- ++msg ;
- mltcmd = FALSE ;
- }
- putcode(msg);
- }
-
- void
- mspeak(num) register int num;
- { (void) fputs(mmsg[num],stdout);
- }
-
- void
- pspeak(obj,prop)
- register struct cvobj *obj;
- register int prop;
- { register char *msg;
- register int i;
-
- msg = obj->props;
- for (i=0; i<prop; ++i) {
- while (*(msg++)) {;}
- }
-
- putcode(msg);
- }
-
-
- int
- yes(quest,agree,refuse) register int quest, agree, refuse;
- {
- auto char answer[8] ;
- int i;
-
- for (;;)
- {
- if (quest) rspeak(quest);
- if (fgets(answer,6,stdin) == NULL) { *answer = '\0'; }
- if (!strlen(answer) || answer[strlen(answer)-1] != '\n') {
- while (getchar() != '\n')
- ;
- answer[0] = '\n'; /* these are error cases */
- }
-
- for (i=0;i<5;++i) {
- if (answer[i] == '\n') {answer[i] = '\0'; break;}
- else answer[i] = tolower(answer[i]);
- }
- if (!strcmp("y",answer) || !strcmp("yes",answer))
- { if (agree) rspeak(agree);
- return(TRUE);
- }
-
- if (!strcmp("no",answer) || !strcmp("n",answer))
- { if (refuse) rspeak(refuse);
- return(FALSE);
- }
-
- (void) fputs("\nPlease answer the question.\n",stdout);
- }
- }
-
- static char cchar = '\0';
- static char *tmpchr;
-
- /* routine to get the character pointed-to by tmpchr.
- * result to cchar
- * and mltcmd turned off if space-comma or space-period.
- */
- static void
- gchar()
- { static char lchar;
- lchar = cchar;
- cchar = *(tmpchr++) ;
- if ((lchar == ' ')
- && ((cchar == '.') || (cchar == ','))
- )
- { mltcmd = FALSE ;
- cchar = '\0' ;
- }
- }
-
- /* gather a word, put terminator in cchar. */
- static char *
- gather()
- { register char *r;
-
- do {gchar();} while (cchar == ' ') ;
- if ((cchar == '\0') || (cchar == '.') || (cchar == ',')) return (NULL) ;
- r = tmpchr;
-
- for (;;)
- { gchar();
- if ((cchar == '\0')
- ||(cchar == '.')
- ||(cchar == ',')
- ||(cchar == ' ') )
- { *(tmpchr-1) = '\0' ;
- return(--r);
- }
- }
- }
-
- void
- getin()
- { extern char *word1, *word2;
-
- static char ca1[81], ca2[81];
-
- register char *cp2, *cp1;
-
- for (;;mltcmd = FALSE)
- { word1 = word2 = NULL ;
- if (mltcmd) {
- if ((cchar != '.') && (cchar != ',')) continue ;
- samvrb = (cchar == ',') ;
- } else {
- if (blklin) fputs("\n",stdout);
- if (fgets(ca1,81,stdin) == NULL)
- { fputs("\nBut, but, ... okay, goodbye.\n",stdout);
- exit(1);
- }
- for (cp1=ca1; *cp1 != '\0'; ++cp1)
- *cp1 = (char) tolower((int)*cp1) ;
- if (*(cp1-1) != '\n')
- { puts("\nThat input was too long... no more than 80\
- characters, please!");
- while (getchar() !='\n')
- ;
- continue; /* this will get us a new input */
- }
- *(cp1-1) = '\0' ; /* alter the final newline */
- mltcmd = TRUE ;
- if ( strcmp(ca1,"again")
- && strcmp(ca1,"repeat")
- && strcmp(ca1,"ditto")
- && strcmp(ca1,"\"")
- && strcmp(ca1,"''") )
- { cp1=ca1, cp2=ca2; /* set to preserve new command */
- } else {
- cp1=ca2, cp2=ca1; /* set to repeat old command */
- }
- while ((*(cp2++) = *(cp1++)) != '\0'); /* end AFTER the null */
- tmpchr = ca1 ;
- samvrb = FALSE ;
- }
- /* now have the input in 'ca1' */
- /* and tmpchr points to the next char to see */
-
- if ((word1 = gather()) == NULL) continue ;
- if (cchar == ' ') word2 = gather();
- return ;
- } /* end of for (implied mltcmd = FALSE) */
- }
-
- int
- vocab(word,sect) register char *word;
- int sect;
- { register int matched = 0, len;
- register struct cvocab *curv = vtab, *likely = NULL ;
-
- len = strlen(word);
-
- if (sect >= 0)
- { for ( ; curv->cvcode != 0; ++curv)
- { if (curv->cvcode / 1000 == sect) break ;
- }
- }
-
- for ( ; curv->cvcode != 0; ++curv)
- { if ((sect >= 0) && ((curv->cvcode / 1000) > sect)) break ;
- ;
- if (!strncmp(word, curv->cvword, len))
- { ++matched;
- likely = curv ;
- if (strlen(curv->cvword) == len) return(curv->cvcode) ;
- }
- }
-
- if (matched == 1) return(likely->cvcode) ;
- else return(-1);
- }
-